<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>登录</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

    <!-- jquery -->
    <script type="text/javascript" th:src="@{/js/jquery.min.js}"></script>
    <!-- bootstrap -->
    <link rel="stylesheet" type="text/css" th:href="@{/bootstrap/css/bootstrap.min.css}"/>

    <link rel="stylesheet" type="text/css" th:href="@{/css/register.css}"/>

    <style>
        .form-control {
            font-size: 10px !important;
        }

        .error {
            font-size: 10px !important;
            color: red !important;
            font-weight: 100 !important;
        }
    </style>
</head>
<body>
<div class="content">
    <div class="title">
        <h4>用户登录</h4>
    </div>

    <form name="loginForm" id="loginForm" method="post">
        <div class="form-group">
            <input id="mobile" name="mobile" class="form-control" type="text" placeholder="手机号码" required="true"
                   minlength="11" maxlength="11"/>
        </div>

        <div class="form-group">
            <input id="password" name="password" class="form-control" type="password" placeholder="密码"
                   required="true" minlength="6" maxlength="16"/>
        </div>
        <div id="div1" name="div1">
            <span id="span1"></span>
        </div>
        <button class="btn btn-primary btn-block" type="button" onclick="to_registerpage()">注册</button>
        <button class="btn btn-primary btn-block" type="button" onclick="to_loginrequest()">登录</button>
    </form>


</div>
</body>
<script type="text/javascript" th:src="@{/bootstrap/js/bootstrap.min.js}"></script>
<!-- jquery-validator -->
<script type="text/javascript" th:src="@{/jquery-validation/jquery.validate.min.js}"></script>
<script type="text/javascript" th:src="@{/jquery-validation/localization/messages_zh.min.js}"></script>
<!-- layer -->
<script type="text/javascript" th:src="@{/layer/layer.js}"></script>
<!-- md5.js -->
<script type="text/javascript" th:src="@{/js/md5.min.js}"></script>
<!-- common.js -->
<script type="text/javascript" th:src="@{/js/common.js}"></script>
<script>
    function to_registerpage() {
        window.location.href = "/user_register.html";
    }

    function to_loginrequest() {
        console.log("前端请求");
        let mobile = $("#mobile").val();
        let inputPassword = $("#password").val();
        let salt = g_passsword_salt;
        let str = salt.charAt(0) + salt.charAt(2) + salt.charAt(4) + inputPassword;
        let formPassword = md5(str);
        $.ajax({
            url: "/user/login",
            type: "POST",
            dataType: "json",
            data: {
                mobile: mobile,
                password: formPassword
            },
            success: function (data) {
                if (data.code == 0) {
                    console.log("用户名密码验证成功");
                    layer.msg("成功");
                    window.location.href = "/goods/list";
                } else {
                    let html = "";
                    html += data.msg;
                    $("#span1").html(html);
                    $("#password").val("");
                    if (data.code == 500214) {
                        $("#mobile").val("");
                    }
                }
            }
            ,
            error: function (data) {
                console.log(data);
                console.log("校验失败");
            }
        });

    }

</script>
</html>